﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Golf.WebUI
{
    public partial class Cart : System.Web.UI.Page
    {
        public DataTable dt = new DataTable();
        public string[] nums = null, pros = null;
        public int CookieCount = 0;
        public string _str1 = "", _str2 = "";
        /// <summary>
        /// 属性 拼接
        /// </summary>
        public string _str3 = "";
        /// <summary>
        /// 属性
        /// </summary>
        public string[] _attr;
        /// <summary>
        /// 键值
        /// </summary>
        public string[] _key;
        Golf.BLL.Golf_Product bllproduct = new Golf.BLL.Golf_Product();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getbind();
            }
        }

        /// <summary>
        /// 加载购物车
        /// </summary>
        public void getbind()
        {
            if (Request.Cookies["GOLF"] != null && Request.Cookies["GOLF"].ToString() != "")
            {
                HttpCookie ShopCart = Request.Cookies["GOLF"];
                _key = ShopCart.Values.AllKeys;
                CookieCount = ShopCart.Values.Count;
                for (int i = 0; i < CookieCount; i++)
                {
                    if (ShopCart.Values[i] == null || ShopCart.Values[i].Trim() == "")
                    {
                        CookieCount = 0;
                        continue;
                    }
                    else
                    {
                        string[] Products = ShopCart.Values[i].Split('|');
                        if (Products.Length > 1)
                        {
                            _str1 += Products[0] + ",";
                            _str2 += Products[1] + ",";
                            _str3 += Products[2] + ",";
                        }
                    }
                }
                if (_str1.Length > 1 & _str2.Length > 1)
                {
                    if (_str1.Substring(_str1.Length - 1, 1) == ",")
                        _str1 = _str1.Substring(0, _str1.Length - 1);
                    if (_str2.Substring(_str2.Length - 1, 1) == ",")
                        _str2 = _str2.Substring(0, _str2.Length - 1);
                    if (_str3.Substring(_str3.Length - 1, 1) == ",")
                        _str3 = _str3.Substring(0, _str3.Length - 1);

                    pros = _str1.Split(new char[] { ',' });
                    nums = _str2.Split(new char[] { ',' });
                    _attr = _str3.Split(new char[] { ',' });
                    //构建表格
                    DataTable _page_dt = new DataTable();
                    _page_dt = bllproduct.GetList("Id in(" + _str1 + ")").Tables[0];//获取商品的详细
                    _page_dt.Columns.Add("_attr", typeof(string));//添加属性列
                    _page_dt.Columns.Add("_Key", typeof(string));//添加cookie Key列  
                    dt.Columns.Add("Id", typeof(string));
                    dt.Columns.Add("Pro_Name", typeof(string));
                    dt.Columns.Add("Pro_Save", typeof(string));
                    dt.Columns.Add("Pro_Price", typeof(string));
                    dt.Columns.Add("Pro_Picture", typeof(string));
                    dt.Columns.Add("Pro_MarketPrice", typeof(string));
                    dt.Columns.Add("Pro_PromotionPrice", typeof(string));
                    dt.Columns.Add("_attr", typeof(string));//添加属性列
                    dt.Columns.Add("_Key", typeof(string));//添加cookie Key列 
                    decimal dSum1 = 0.00M, price1 = 0.00M;
                    for (int i = 0; i < _page_dt.Rows.Count; i++)
                    {
                        price1 = decimal.Parse(_page_dt.Rows[i]["Pro_Price"].ToString());
                        DataRow dr = _page_dt.Rows[i];
                        //循环匹配 商品
                        for (int j = 0; j < pros.Length; j++)
                        {
                            int num = int.Parse(nums[j]);
                            dSum1 += price1 * num;
                            DataRow dr1 = dt.NewRow();
                            if (pros[j] == dr["Id"].ToString())
                            {
                                dr1["_attr"] = Server.UrlDecode(_attr[j]);//添加该商品的属性
                                dr1["_Key"] = ShopCart.Values.AllKeys[j];//添加该商品的键值 
                                dr1["Id"] = _page_dt.Rows[i]["Id"].ToString();
                                dr1["Pro_Name"] = _page_dt.Rows[i]["Pro_Name"].ToString();
                                dr1["Pro_Save"] = _page_dt.Rows[i]["Pro_Save"].ToString();
                                dr1["Pro_Price"] = _page_dt.Rows[i]["Pro_Price"].ToString();
                                dr1["Pro_Picture"] = _page_dt.Rows[i]["Pro_Picture"].ToString();
                                dr1["Pro_MarketPrice"] = _page_dt.Rows[i]["Pro_MarketPrice"].ToString();
                                dr1["Pro_PromotionPrice"] = _page_dt.Rows[i]["Pro_PromotionPrice"].ToString();
                                dt.Rows.Add(dr1);
                            }
                        }
                    }
                    //总金额
                    this.ActualPrice.InnerHtml = "$" + dSum1.ToString("0.00");
                    dt.Dispose();
                }
            }
        }

        /// <summary>
        /// 获取数量
        /// </summary>
        public string getProNum(string proId)
        {
            string _strBack = "1";
            for (int i = 0; i < pros.Length; i++)
            {
                if (_key[i] == proId)
                {
                    _strBack = nums[i];
                }
            }
            return _strBack;
        }

        /// <summary>
        /// 小计金额
        /// </summary>
        public string getProPrice(string price, string proNums)
        {
            decimal _totle = 0.00M;
            _totle = decimal.Parse(price) * decimal.Parse(proNums);
            return _totle.ToString("0.00");
        }
    }
}
